package com.openstack.javasdk;

import com.alibaba.fastjson.JSONObject;
import org.openstack4j.api.OSClient.OSClientV3;
import org.openstack4j.model.common.Identifier;
import org.openstack4j.model.identity.v3.Endpoint;
import org.openstack4j.model.identity.v3.Role;
import org.openstack4j.model.identity.v3.Service;
import org.openstack4j.model.identity.v3.User;
import org.openstack4j.openstack.OSFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/users")
public class UserModule {


//    You are not authorized to perform the requested action: identity:list_users.
//     添加               .scopeToProject(Identifier.byId("e0af27432aee4d74b88484c3779c3e42"))
//    解决
    @GetMapping("/all")
    public String all(){
        OSClientV3 os = Auth.getOS();
        List<? extends User> userList = os.identity().users().list();
        return JSONObject.toJSONString(userList);
    }

    @GetMapping("/roleList")
    public String roleList(){
        OSClientV3 os = Auth.getOS();
        List<? extends Role> roleList = os.identity().roles().list();
        return JSONObject.toJSONString(roleList);
    }

    @GetMapping("/serviceList")
    public String serviceList(){
        OSClientV3 os = Auth.getOS();
        List<? extends Service> serviceList = os.identity().serviceEndpoints().list();
        return JSONObject.toJSONString(serviceList);
    }

    @GetMapping("/endpointList")
    public String endpointList(){
        OSClientV3 os = Auth.getOS();
        List<? extends Endpoint> endpointList = os.identity().serviceEndpoints().listEndpoints();
        return JSONObject.toJSONString(endpointList);
    }
}
